<?php
/**
 * 关系系统处理类
 * 这里有一个问题是：PEOPLE原是作为关系系统中标识一个角色的唯一键
 * 但目前在对前端的交互中，PEOPLE对前端是隐藏的。
 * 这在初始以前端为通讯中心的结构中意义是较小的。
 * 而在后期更换为MQ实现异步发送消息时，需要对这部分的数据结构进行更改，
 * 以使得MQ中的脚本可以直接读取以PEOPLE为唯一键的列表类型的存取系统
 *
 * @author ananda
 */
class Model_Relation extends Model
{
    public $self_name = 'Relation';

    public $conn = 'ttcache';

    public $tt_prefix = 'relation:';

    public $to_suffix   = 'to';
    public $from_suffix = 'from';

    public $no_init_mode = true;

    public function init()
    {
        $this->conn = Connection::get($this->conn);
    }

    public function generateToKey($people)
    {
        return $this->tt_prefix."$people.$this->to_suffix";
    }

    public function generateFromKey($people)
    {
        return $this->tt_prefix."$people.$this->from_suffix";
    }

    public function initToData()
    {
        return array();
    }

    public function initFromData()
    {
        return array();
    }

    public function register($people)
    {
        $this->conn->create($this->generateToKey($people), $this->initToData());
        $this->conn->create($this->generateFromKey($people), $this->initFromData());
        return true;
    }

    public function clean($people)
    {
        $this->conn->delete($this->generateToKey($people));
        $this->conn->delete($this->generateFromKey($people));
        return true;
    }

    protected function createRecord($user_id, $createdat)
    {
        return array(
            'uid' => $user_id,
            'createdat' => $createdat,
        );
    }

    public function append($people_from, $people_to, $user_id, $target_user_id, $timestamp = 1)
    {
        // 修改自己
        $key  = $this->generateFromKey($people_from);
        $data = $this->getList($key);
        if (Error::is($data)) {
            return $data;
        }
        $data[$target_user_id] = $this->createRecord($target_user_id, $timestamp);
        $result = $this->conn->update($key, $data);
        if (Error::is($result)) {
            return $result;
        }

        // 修改他人
        $key  = $this->generateToKey($people_to);
        $data = $this->getList($key);
        if (Error::is($data)) {
            return $data;
        }
        $data[$user_id] = $stamp;
        $result = $this->conn->update($key, $data);
        if (Error::is($result)) {
            return $result;
        }

        return true;
    }

    public function cancel($people_from, $people_to, $user_id, $target_user_id)
    {
        // 修改自己
        $key  = $this->generateFromKey($people_from);
        $data = $this->getList($key);
        unset($data[$target_user_id]);
        $this->conn->update($key, $data);

        // 修改他人
        $key  = $this->generateToKey($people_to);
        $data = $this->getList($key);
        unset($data[$user_id]);
        $this->conn->update($key, $data);

        return true;
    }

    public function getList($key)
    {
        $data = $this->conn->readOne($key);
        if (Error::is($data) && $this->no_init_mode) {
            $result = array();
            return new Data_ArrayAccess($result);
        }
        return $data;
    }
}
